package com.csweimei.rentmanagementtreasure.enterprise.viewModel;

import android.app.Application;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.databinding.ObservableArrayList;
import androidx.databinding.ObservableField;
import androidx.databinding.ObservableList;

import com.chad.library.BR;
import com.csweimei.rentmanagementtreasure.R;
import com.csweimei.rentmanagementtreasure.http.entity.StaffListBean;
import com.csweimei.rentmanagementtreasure.pop.PopUtils;
import com.csweimei.rentmanagementtreasure.pop.PopWheelBase;
import com.csweimei.rentmanagementtreasure.viewModel.TitleVM;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

import io.reactivex.observers.DisposableObserver;
import me.goldze.mvvmhabit.base.BaseModel;
import me.goldze.mvvmhabit.binding.command.BindingAction;
import me.goldze.mvvmhabit.binding.command.BindingCommand;
import me.goldze.mvvmhabit.utils.RxUtils;
import me.tatarka.bindingcollectionadapter2.ItemBinding;

public class StaffManagerVM extends TitleVM {
    public StaffManagerVM(@NonNull Application application, BaseModel model) {
        super(application, model);
        setTitleText("员工管理");
    }

    public ObservableField<String> companyID = new ObservableField<>();
    public ObservableField<String> keyword = new ObservableField<>();
    public ObservableField<String> px = new ObservableField<>("全部");

    @Override
    public void onResume() {
        super.onResume();
        getData();
    }

    public ObservableField<Integer> showNo = new ObservableField<>();
    public ObservableList<StaffListItemVM> observableList = new ObservableArrayList<>();
    public ItemBinding<StaffListItemVM> itemBinding = ItemBinding.of(BR.viewModel, R.layout.item_staff);

    private void getData() {
        Map<String,String> map = new HashMap<>();
        map.put("companyID",companyID.get());
        map.put("keyword",keyword.get());
        map.put("px",px.get());
        model.stafflist(map)
                .compose(RxUtils.schedulersTransformer())
                .compose(RxUtils.exceptionTransformer())
                .doOnSubscribe(this)
                .subscribe(new DisposableObserver<StaffListBean>() {
                    @Override
                    public void onNext(StaffListBean o) {
                        if (o.issucc){
                            observableList.clear();
                            if (o.getData().size() > 0){
                                showNo.set(View.GONE);
                                for (StaffListBean.DataDTO bean: o.getData()) {

                                    StaffListItemVM itemVM = new StaffListItemVM(StaffManagerVM.this);
                                    itemVM.setData(bean);
                                    observableList.add(itemVM);
                                }
                            }else {
                                showNo.set(View.VISIBLE);
                            }
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        showNo.set(View.VISIBLE);
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }


    public BindingCommand showPx = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            PopWheelBase popWheelBase = new PopWheelBase(mActivity, 1, s -> {
                px.set(s);
                onResume();
            });
            popWheelBase.setList1(Arrays.asList(mActivity.getResources().getStringArray(R.array.staff_px)));
            PopUtils.showPop(mActivity,popWheelBase,false);

        }
    });
}
